package search;

import treeview.unit.AbstractUnitView;
import treeview.unit.ImageView;
import treeview.unit.TextView;

import java.util.TreeMap;

/**
 * Created by Pavel Nikolaev
 */
public class OffsetSearcher extends LeafVisitor {
    private TreeMap<Integer, TextView> myData;

    public OffsetSearcher(LeafIndexer indexer) {
        myData = new TreeMap<Integer, TextView>();
        update(indexer);
    }

    public void update(LeafIndexer indexer) {
        myData.clear();
        for (AbstractUnitView leaf : indexer.getIndexedViews()) leaf.accept(this);
    }

    public void visitTextView(TextView view) {
        myData.put(view.getStartOffset(), view);
    }

    public void visitImageView(ImageView view) {
        /*ignored*/
    }

    public void visitUnitView(AbstractUnitView view) {
        /*ignored*/
    }

    public TextView getTextView(int offset) {
        java.util.Map.Entry<Integer, TextView> entry = myData.lowerEntry(offset);
        if (entry == null) {
            int look = 0;
        }
        return entry.getValue();
    }
}
